import Vue from 'vue';
import * as appUtils from './appUtils';
import axios from 'axios';
import router from './../router';
import {
	API_URL,
} from '../../env.js'

let baseUrl = API_URL
// let baseUrl = 'http://127.0.0.1:7009'

let custySwitch = true  //简单的控制打包的时候 是测试还是生产
//生产环境
if (process.env.NODE_ENV !== "development" && custySwitch) {
  baseUrl = 'https://vlog.sj0763.com'
}
axios.defaults.baseURL = baseUrl







Vue.prototype.axios = axios;


export const HttpServer = {
  get: function get(url, data={}, showTips = false,needToken = true) {
    let promise = new Promise(function (resolve, reject) {
      let headers = setHeader(needToken);
      axios.get(url, {params: data, 'headers': headers}, showTips).then((res) => {
        if (res.data.code == 1) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
        showMessage(res.data, showTips);
      }).catch((err) => {
        router.push({
          path: '/login'
        });
        reject(err.response.data);
        showMessage({'status': -1, 'message': '网络错误'}, showTips);
      });
    });
    return promise;
  },
  post: function post(url, data={}, showTips = false,needToken = true, header = false) {
    let promise = new Promise(function (resolve, reject) {
      let headers = setHeader(needToken);
      if (header){
        headers = header
      }
      axios.post(url, data, {'headers': headers}).then((res) => {
        if (res.data.code == 1) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
        showMessage(res.data, showTips);
      }).catch((err) => {
        router.push({
          path: '/login'
        });
        showMessage({'status': -1, 'message': '网络错误'}, showTips);
        reject(err.response.data);
      });
    });
    return promise;
  },
  put: function put(url, data={}, showTips = false,needToken = true) {
    let promise = new Promise(function (resolve, reject) {
      let headers = setHeader(needToken);
      axios.put(url, data, {'headers': headers}).then((res) => {
        if (res.data.code == 1) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
        showMessage(res.data, showTips);
      }).catch((err) => {
        router.push({
          path: '/login'
        });
        showMessage({'status': -1, 'message': '网络错误'}, showTips);
        reject(err.response.data);
      });
    });
    return promise;
  },
  delete: function delet(url, data={}, showTips = false,needToken = true) {
    let promise = new Promise(function (resolve, reject) {
      let headers = setHeader(needToken);
      axios.delete(url, {params: data, 'headers': headers}, showTips).then((res) => {
        if (res.data.code == 1) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
        showMessage(res.data, showTips);
      }).catch((err) => {
        router.push({
          path: '/login'
        });
        reject(err.response.data);
        showMessage({'status': -1, 'message': '网络错误'}, showTips);
      });
    });
    return promise;
  },
};
function setHeader(needToken) {
  let headers = {
    'Content-Type': 'application/json'
  };
  Object.assign(headers, {'Authorization': 'Bearer ' + appUtils.getToken()});
  return headers;
}
function showMessage(res, showTips) {
  if (!res) {
    return;
  }
  let type = '';
  switch (Number(res.code)) {
    case 401:
      type = 'error';
      res.message = '登录超时，请重新登录';
      router.push({
        path: '/login'
      });
      break;
    default:
      type = 'error';
      break;
  }
}

